	   
** Fig 1
clear
set obs 10000
gen z = _n*100

***** 2005
gen dedu = min(min(57400, max(z*.31,31800)) , 57400) 
                    
*-* Tax base and calculated taxes
gen gross = z  
gen ordinary0 = max(gross  - dedu  , 0)              
gen ordinary1 = max(ordinary0  - 34200 , 0) 

gen sic  = max(min([.078*z]*(gross>29600) , 0.25*[gross-29600]) , 0) 
gen flat = ordinary1*.28 
gen surtax = max(gross-381000,0)*.12 + max(gross-800000,0)*.035 
                    
*-* Total income tax 
gen itax2005 = sic + flat + surtax  
gen  av2005 = itax2005/z
gen ma2005 = (itax2005-itax2005[_n-1])/(z-z[_n-1])
drop gross ordinary0 ordinary1 sic flat surtax dedu               


gen dedu = 0
                    
*-* Tax base and calculated taxes
gen gross = z  
gen ordinary0 = max(gross  - dedu  , 0)              
gen ordinary1 = max(ordinary0  - 34200 , 0) 

gen sic  = max(min([.107*z]*(gross>29600) , 0.25*[gross-29600]) , 0) 
gen flat = ordinary1*.28 
gen surtax = max(gross-381000,0)*.12 + max(gross-800000,0)*.035 
                    
*-* Total income tax 
gen itax2005_se = sic + flat + surtax  
gen  av2005_se = itax2005_se/z
gen ma2005_se = (itax2005_se-itax2005_se[_n-1])/(z-z[_n-1])
drop gross ordinary0 ordinary1 sic flat surtax dedu 


replace z = z/1000
replace ma2005 = ma2005*100
replace av2005 = av2005*100
replace ma2005_se = ma2005_se*100
replace av2005_se = av2005_se*100

replace ma2005 = round(ma2005,.1)
replace ma2005_se = round(ma2005_se,.1)



twoway	( line ma2005 z , lcolor(black) lpattern(solid) lwidth(medthick)) ( line ma2005_se z , lcolor(black) lpattern(longdash) lwidth(medthick)), ///
		scheme(s1color) legend(label(1 "Wage Income") label(2 "Self Employment Income") label(3 "2005") label(4 "2010")) ///
		ytitle("Marginal tax rate, per cent") xtitle("Thousands of 2005 NOK ")
graph export ".../Figure1.pdf",replace 
graph save ".../Figure1",replace 

		
	   
** Fig 2 and Fig 3	   

clear
set obs 10000
gen z = _n*100





// 1995
gen dedu = min(min(28700*1.589926, max(z*.2,3500*1.589926)) , 28700*2.589926) 

gen gross = z 
gen ordinary0 = max(gross - dedu  , 0)              
gen ordinary1 = max(ordinary0 - 22600*1.589926, 0) 

gen sic = max(min([.078*z]*(gross>(17000*1.589926)) , 0.25*[gross-(17000*1.589926)]) , 0) 
gen flat = ordinary1*.28 
gen surtax = max(gross-(212000*1.589926),0)*.095 + max(gross-(239000*1.589926),0)*.042 
                    
// Total income tax 
gen itax1995 = sic + flat + surtax 
*average marginal                     
gen  av1995 = itax1995/z
gen ma1995 = (itax1995-itax1995[_n-1])/(z-z[_n-1])
drop gross ordinary0 ordinary1 sic flat surtax dedu
 


***** 2005
gen dedu = min(min(57400, max(z*.31,31800)) , 57400) 
                    
*-* Tax base and calculated taxes
gen gross = z  
gen ordinary0 = max(gross  - dedu  , 0)              
gen ordinary1 = max(ordinary0  - 34200 , 0) 

gen sic  = max(min([.078*z]*(gross>29600) , 0.25*[gross-29600]) , 0) 
gen flat = ordinary1*.28 
gen surtax = max(gross-381000,0)*.12 + max(gross-800000,0)*.035 
                    
*-* Total income tax 
gen itax2005 = sic + flat + surtax  
gen  av2005 = itax2005/z
gen ma2005 = (itax2005-itax2005[_n-1])/(z-z[_n-1])
drop gross ordinary0 ordinary1 sic flat surtax dedu               

*** 2010

gen dedu = min(min(72800*.79346709, max(z*.36,31800*.79346709)) , 72800*.79346709) 
                    
*-* Tax base and calculated taxes
gen gross = z   
gen ordinary0 = max(gross - dedu  , 0)              
gen ordinary1 = max(ordinary0  - 42210*.79346709 , 0) 

gen sic  = max(min([.078*z]*(gross>(39600*.79346709)) , 0.25*[gross-(39600*.79346709)]) , 0) 
gen flat = ordinary1*.28 
gen surtax = max(gross-456400*.79346709,0)*.09 + max(gross-741700*.79346709,0)*.03 
                    

gen itax2010 = sic + flat + surtax  
gen  av2010 = itax2010/z
gen ma2010 = (itax2010-itax2010[_n-1])/(z-z[_n-1])
drop gross ordinary0 ordinary1 sic flat surtax dedu               


replace z = z/1000
replace av1995 = av1995*100
replace ma1995 = ma1995*100
replace ma2010 = ma2010*100
replace av2010 = av2010*100
replace ma2005 = ma2005*100
replace av2005 = av2005*100

replace ma1995 = round(ma1995,.1)
replace ma2005 = round(ma2005,.1)
replace ma2010 = round(ma2010,.1)

		
		
	
		
twoway	(line ma1995 z if z>250, lcolor(black)  lpattern(solid) )   ///
		( line ma2005 z if z>250, lcolor(black) lpattern(longdash) lwidth(medthick)) ( line  ma2010 z if z>250 , lcolor(black) lpattern(shortdash_dot) lwidth(medthick)), ///
		scheme(s1color) legend(label(1 "1995")  label(2 "2005") label(3 "2010")) ///
		ytitle("Marginal tax rate, per cent") xtitle("Wage Income, 2005 NOK, thousands ")
graph export ".../Figure2.pdf",replace 
graph save ".../Figure2",replace 


		
twoway	(line av1995 z, lcolor(black)  lpattern(solid) )  ///
		( line av2005 z, lcolor(black) lpattern(longdash) lwidth(medthick)) ( line av2010 z , lcolor(black) lpattern(shortdash_dot) lwidth(medthick)), ///
		scheme(s1color) legend(label(1 "1995")  label(2 "2005") label(3 "2010")) ///
		ytitle("Average tax rate, per cent") xtitle("Wage Income, 2005 NOK, thousands ")
graph export ".../Figure3.pdf",replace 
graph save ".../Figure3",replace 




****figure 4 

clear all
set more off
cap log close
version 13
local gruppo = 1


// global paths 
global datainpath "..."
global dataoutpath "..."
global graphpath "..." 
global outputpath "..."

global wijioutput "..."


use "${dataoutpath}left_cens_spell_10sample_all_4type_`gruppo'_rev1.dta", clear    /* changed 13 June 2019 */
drop if ban_`gruppo' == 1
rename sedum_`gruppo' sedum

tab1 lm_status_1 sedum


foreach  i of numlist 1993/2011 {
sum sedum if year==`i' 
scalar sedum_`i'=r(mean)
}
preserve
clear 
set obs 19
gen year=_n+1992
gen sedum=0
foreach  i of numlist 1993/2011 {
replace sedum=sedum_`i'*100 if year==`i'
}
twoway (line sedum year, lcolor(black)), scheme(s1color) ytitle("Share of Self-Emploment, percent") ylabel(0(2)10)
graph export ".../Figure4.pdf",replace 
graph save ".../Figure4",replace 
restore

****figure 5 nd 6, to be run after 7_estimation_sample_and_variables.do


cap log close	
set more off	

version 13	
use /ssb/stamme04/ekstern/wia/wk48/sample_estimation_1_03Sept2020.dta, clear


twoway (histogram netincdiff if netincdiff>-1.2 & netincdiff<.5 [pweight=pweight] fraction, fcolor(gray) lcolor(white)  ), ///
	scheme(s1color) xtitle("{it:Netincdiff}") graphregion(color(white)) xlabel(-1.2(.2).5)
graph export ".../Figure5.pdf",replace 
graph save ".../Figure5",replace 


twoway (histogram convexity_naer if convexity_naer>-.02 & convexity_naer<.05 [pweight=pweight]    , fcolor(gray) lcolor(white) fraction width(0.0005)), ///
	scheme(s1color) xtitle("{it:Convexity}")  xlabel(-.02(.01).05) //
graph export ".../Figure6.pdf",replace 
graph save ".../Figure6",replace 



*** figure 7 and 8, to be run after 6_netincdiff.do

version 13
set more off

use  ".../left_cens_spell_10sample_10never_type_1_Sept2020_hmle.dta", clear


gen convexity_naer_SE = convexity_naer if sedum == 1 
gen convexity_naer_WE = convexity_naer if sedum == 0 

gen netincdiff_SE = netincdiff if sedum == 1
gen netincdiff_WE = netincdiff if sedum == 0

label variable convexity_naer_SE "Self-Employment"
label variable convexity_naer_WE "Employment"
label variable netincdiff_SE "Self-Employment"
label variable netincdiff_WE "Employment"


graph box netincdiff, ///
	nooutsides over(year, label( angle(45))) scheme(s1color) ytitle("{it:Netincdiff}") box(1, color(gs5) lpattern(dash) ) 
graph export ".../Figure7.pdf",replace 
graph save ".../Figure7",replace 

graph box convexity_naer, /// 
     nooutsides over(year, label( angle(45))) scheme(s1color) ytitle("{it:Convexity}") box(1, color(gs5) lpattern(dash) )
graph export ".../Figure8.pdf",replace 
graph save ".../Figure8",replace 


**** Figure 9, to be run after 7_estimation_sample_and_variables.do
cap log close	
set more off	

version 13	
use /ssb/stamme04/ekstern/wia/wk48/sample_estimation_1_03Sept2020.dta, clear
capture drop a_end a_seq a_spell
tsset id year, y
tsspell lm_status, end(a_end) seq(a_seq) spell(a_spell)
tab a_seq, gen(durdum)
*expand obs
expand 10 if  pweight == 10

* regression for SE
regress durvar durdum2-durdum17 if lcens_spell ==0 & sedum==1 
scalar durdum_se_1 = _b[_cons]      
forvalues k = 2(1)17 {
    scalar durdum_se_`k' = durdum_se_1 + _b[durdum`k']
}
preserve
clear 
set obs 17
gen coeffhat = .
gen duration = _n
forvalues h = 1(1)17 {
    replace coeffhat = durdum_se_`h' in `h'
}
restore


* regression for WE
regress durvar durdum2-durdum17 if lcens_spell ==0 & sedum==0 
scalar durdum_e_1 = _b[_cons]       
forvalues k = 2(1)17 {
    scalar durdum_e_`k' = durdum_e_1 + _b[durdum`k']
}
preserve
clear 
set obs 17
gen coeffhate = .
gen coeffhatse = .
gen duration = _n
forvalues h = 1(1)17 {
    replace coeffhate = durdum_e_`h' in `h'
    replace coeffhatse = durdum_se_`h' in `h'
}

label variable coeffhatse "Self-Employment Hazard"
label variable coeffhate "Wage-Employment Hazard"

twoway (scatter coeffhate duration, msize(medium) m(+) color(black)) (scatter coeffhatse duration, msize(small) color(black)) ,  scheme(s1color) ytitle("Probability") xtitle("Duration in Years") xlabel(1(4)17) xscale(range(1 17))
graph export ".../Figure9.pdf",replace 
graph save ".../Figure9",replace 
restore

*** figure 10
clear
set obs 10000
gen z = _n*100
 
 
//2000                  
*-* Deductions 
gen dedu = min(min(36600, max(z*.22,30600)) , 36600) 
                    
*-* Tax base and calculated taxes
gen gross = z   
gen ordinary0 = max(gross- dedu  , 0)              
gen ordinary1 = max(ordinary0 - 27700 , 0) 

gen sic  = max(min([.078*z]*(gross>(22200)) , 0.25*[gross-(22200)]) , 0) 
gen flat = ordinary1*.28 
gen surtax = max(gross-277800,0)*.135 + max(gross-762700,0)*.06 
                    
*-* Total income tax 
gen itax2000 = sic + flat + surtax  
gen  av2000 = itax2000/z
gen ma2000 = (itax2000-itax2000[_n-1])/(z-z[_n-1])
drop gross ordinary0 ordinary1 sic flat surtax dedu               


gen dedu = min(min(36600, max(z*.22,30600)) , 36600) 
                    
*-* Tax base and calculated taxes
gen gross = z   
gen ordinary0 = max(gross- dedu  , 0)              
gen ordinary1 = max(ordinary0 - 27700 , 0) 

gen sic  = max(min([.078*z]*(gross>(22200)) , 0.25*[gross-(22200)]) , 0) 
gen flat = ordinary1*.28 
gen surtax = max(gross-200000,0)*.11
                    
*-* Total income tax 
gen itax2000_flat = sic + flat + surtax  
gen  av2000_flat = itax2000_flat/z
gen ma2000_flat = (itax2000_flat-itax2000_flat[_n-1])/(z-z[_n-1])
drop gross ordinary0 ordinary1 sic flat surtax dedu               


replace z = z/1000
replace ma2000 = ma2000*100
replace av2000 = av2000*100
replace ma2000_flat = ma2000_flat*100
replace av2000_flat = av2000_flat*100


replace ma2000 = round(ma2000,.1)
replace ma2000_flat = round(ma2000_flat,.1)
twoway	(line ma2000_flat z , lcolor(black)  lpattern(solid) )  (line ma2000 z , lcolor(black) lpattern(dash) lwidth(medthick)), ///
		scheme(s1color) legend(label(1 "2000 flat") label(2 "2000 baseline"))  ///
		ytitle("Marginal tax rate, per cent") xtitle("Wage Income, 2000 NOK, thousands ")
		
graph export ".../Figure10.pdf",replace 
graph save ".../Figure10",replace 

		

	
